Git It Done

2017-06-19

The amount of times I look these up...
I have screenshots saved, I have notes...
Yea, I'll be adding to this list. Until then...

Switching Branches

$ git checkout -b branch-name-here
switches to new branch 'branch-name-here'

Shortcut for:

$ git branch branch-name-here
$ git checkout branch-name-here

Using Rebase to Update Branch

$ git checkout master
$ git pull
$ git checkout other-branch
$ git rebase master
$ git push origin other-branch -f

When you need to delete a branch

$ git branch -d the-local-branch

When you want to delete untracked files from working branch

This one I actually only used once and I'm not exactly sure what was the situation, but I jotted it down nonetheless because, well, you never know. There are two posts I saved regarding this topic-- perhaps, I'll look them over and summarize them here or on a seperate post.

$ git clean -n (will show files that would be deleted)

$ git clean -f (will delete files)

When you screw up a git merge

$ git reset --hard master@{"300 minutes ago"} to revert back to a certain period of time... before you screwed up :D

That^ little nugget of wisdom came from @data_stephanie on Twitter. I love Twitter

Undo local commit

$ git reset --hard HEAD^ undo without keeping changes made

$ git reset --soft HEAD^ undo keeps changes BEFORE commit

Update

First time using this one was to remove a few deleted files.

$ git add -u stages already tracked files, does not add new files

How do I get out of a git log situation?

ready? press Q key. Yep.

Amend most recent commit message, not pushed

$ git commit --amend that will open editor or git commit --amend -m "new commit message here" straight from terminal.

link below gives more detail and talks of modifying commit message when already pushed.

more info

So what about git stash?

check out the stash using git stash list

for the latest stash, git stash apply and for a non-recent stash... it will look something like git stash apply stash@{2} the number will depend on what is displayed when you check out the list and which one of those you want to apply. Note: using no number, it will be assumed it is the latest stash you're looking to apply.

how about gettin' rid of that stash? git stash drop drops the top stash, while git stash drop stash@{2} gets rid of a specific stash. Note: the '2' is just for example, it can be whatever number that points to the specific stash your heart desires.

That time you made some commits and realized you forgot to make an alternate branch so you don't efF your master... /facepalm

create a new branch, $ git branch branch-name-here, from where you've made all the commits, in my case it was master

make sure you're on whatever branch you made the mistake commits too... make sure... are you sure? Ok.

$ git reset --hard 8abc8ab the characters after --hard will be the commit you'd like to revert to.

This will revert the branch you accidentally commited to back to the point(commit) before the screw up :D Note: you can use first 7 characters of the commit, they can be forever long

When you update a readme or something similar through github, but forget to do a git pull and update your local repo and then you go do stuff and go to push, but...

git pull --rebase adds your new commit on top, at this point you should see some stuff in your terminal saying to re-push your awesome :D; your github should show commits in the order expected

When you get "please enter a commit message to explain why this merge is necessary..."

sometimes this happens where an editor will show up that (if you've never seen before) may leave you staring at the screen all 'dafuq-like'.

If you happen to go this route:

  1. press "i"
  2. write your merge message
  3. press "esc"
  4. write ":wq"
  5. then press enter

That gem of info (list) came from this SO post

So you wanna re-snag your repo or snag to contribute to one...

Hello git clone! Put that into your terminal, at your desired location, and follow it with url that shows up when you click that "clone or download" green button on repo's page. See this nice post by Github :)

Resource: Forking vs. Cloning repo

You add, you commit, you push and then... you realize you need to revert back to a previous commit on your remote repo O_O

git reset --hard <commit-hash>
git push -f origin master

Note: DO NOT do this if you have a repo with collaborators who may have pulled and started working. At that point it may be best to create a branch with what you want back on there to then be merged into master repo. That way everyone pulls that update with their possible additions jazz and things :D

Resource: Resetting Remote to a Certain Commit

When you get something like "Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit(s) each, respectively."

Diverged Branches SO Link has a few links to look into within the accepted answer.

Fixed conflicts, but git still complains

  • fix conflict
  • git add <file>
  • no commit between add and rebase**
  • git rebase --continue

Resource: After Fixing Conflicts Git Still Complains

Multiple Github Accounts

One day you may find yourself needing to keep things seperate... work and personal accounts for instance.

Reference:

Note: One thing I noticed when I had to do this some time after setup-- if I wasn't cloning but creating you still have to make sure you differentiate cmd as would with cloning and check/change user/email associated to gh acct desired.

git config --list to check username/email for particular project

  • change if you must
  • make sure when you're seting the repo you are using the correct format (see: differentiate repo/accts above) git remote set-url origin git://new.url.here

Reference: how-to-remove-remote-origin-from-git-repo

Note: SSH keys get deleted after a certain period of inactivity pbcopy < ~/.ssh/someName.pub -- if you have .pub files you're able to copy this to connect to GitHub

Need to transfer commits from one repo to another? (via cherry-pick)

Not limited to remote on another machine.

cd {repo to update} git remote add {path of repo cherry-picking from}

You can fetch and cherry-pick commits to transfer, ie: git cherry-pick abc6666^..xyz8888. Note: the use of the ^ is to include that first commit in the range, otherwise it will start the following commit in the range

Regarding cherry-pick: If you press git cherry-pick --continue and you have resolved conflicts and added files and all that jazz... you may see something that looks like could be an error when in fact it is your default editor.

Navigate using:

  • press "i"
  • write your merge message
  • press "esc"
  • ":wq"
  • then press enter

Reference:

Make pretty git logs

git log --oneline --graph

Below discoveries came from looking up warning: LF will be replaced by CRLF in <whatever file> which apparently happens when working on a Windows machine, although I had never seen it before this moment shrugs anywaaay...

Undo git init .

rm -rf .git

Undo first (following git init) git add .

git rm -r --cached .

What I ended up doing for my initial issue: git config --local core.autocrlf false there's like a whole thing to this... I mean a whole thing where there's arguments to both sides, true and false. Instead of the rabbit hole I would have for sure continued on I decided on that and moved on. Although, I'd be lying if I didn't say... I'm curious.

Removing file with sensitive data

I ended up not ignoring a file I should have and then realized it after a few commits... luckily, there is... git filter-branch; Resource: https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository

Note: if it doesn't seem to work, check file path


← go back